#include "stdafx.h"
#include "SVMScaler.h"

#include <fstream>
#include <cmath>
using namespace std;

SVMScaler::SVMScaler(const char* rangeFileName)
{
	fstream file;
	file.open( rangeFileName, ios::in );

	char x;
	int index;
	double lowerBound, higherBound;
	file >> x >> lowerBound >> higherBound;
	
	
	m_ranges.reserve( 4000 );

	while( file >> index )
	{
		file >> lowerBound >> higherBound;
		m_ranges.push_back( pair<double,double>( lowerBound, higherBound ) );
	}

	file.close();
}

SVMScaler::~SVMScaler()
{
	m_ranges.clear();
}

void SVMScaler::Scale(std::vector<double> &input)
{
	if( input.size() != m_ranges.size() )
		throw std::exception( "Scale dimension not matches" );

	const double epsilon = 0.0001;
	const int length = (int)input.size();

	for( int i = 0; i < length; ++i )
	{
		if( fabs( m_ranges[i].second - m_ranges[i].first ) > epsilon )
		{
			input[i] = ( input[i] - m_ranges[i].first ) / ( m_ranges[i].second - m_ranges[i].first );
			input[i] = input[i] * 2 - 1;
		}
	}
}
